package tw.com.hitevision.whiteboard.android.stateFile;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;

import tw.com.hitevision.whiteboard.android.render.Autoshape;
import tw.com.hitevision.whiteboard.android.render.Group;
import tw.com.hitevision.whiteboard.android.render.Highlighter;
import tw.com.hitevision.whiteboard.android.render.Image;
import tw.com.hitevision.whiteboard.android.render.Pen;
import tw.com.hitevision.whiteboard.android.render.StickyNote;
import tw.com.hitevision.whiteboard.android.render.Word;

public class BaseStateFileAdapter implements JsonDeserializer<BaseStateFile> {
    @Override
    public BaseStateFile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

        JsonObject jsonObject = json.getAsJsonObject();
        String className = jsonObject.get("className").getAsString();

        if (className.equals(Pen.class.getSimpleName())) {
            return GsonInstance.getGsonInstance().fromJson(json, PenStateFile.class);
        } else if (className.equals(Highlighter.class.getSimpleName())) {
            return GsonInstance.getGsonInstance().fromJson(json, HighlighterStateFile.class);
        } else if (className.equals(Autoshape.class.getSimpleName())) {
            return GsonInstance.getGsonInstance().fromJson(json, AutoShapeStateFile.class);
        } else if (className.equals(Word.class.getSimpleName())) {
            return GsonInstance.getGsonInstance().fromJson(json, WordStateFile.class);
        } else if (className.equals(StickyNote.class.getSimpleName())) {
            return GsonInstance.getGsonInstance().fromJson(json, StickyNoteStateFile.class);
        } else if (className.equals(Group.class.getSimpleName())) {
            return GsonInstance.getGsonCustomInstance().fromJson(json, GroupStateFile.class);
        } else if (className.equals(Image.class.getSimpleName())) {
            return GsonInstance.getGsonCustomInstance().fromJson(json, ImageStateFile.class);
        }

        return null;
    }
}
